/**
 *  Exprezoo Records
 *  Copyright (C) 2012 Sergio De Mattia
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 */
package com.sdmdev.android.exprezoorecords.json.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.sdmdev.android.exprezoorecords.R;
import com.sdmdev.android.exprezoorecords.R.string;
import com.sdmdev.android.exprezoorecords.activity.HomePageActivity;
import com.sdmdev.android.exprezoorecords.utility.ApplicationProperties;
import com.sdmdev.android.exprezoorecords.utility.JSONfunctions;
import com.sdmdev.android.exprezoorecords.utility.Networking;


public class ArtistsActivity extends ListActivity{
	JSONArray artistsList = null;
	private static final String TAG = "ArtistsActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholderevents);
        TextView titleTv = (TextView)findViewById(R.id.title);
        titleTv.setText(R.string.titleartists);
        
        if(Networking.isNetworkAvailable(ArtistsActivity.this))
        	new BackgroundWorker().execute();	
        else
            Toast.makeText(ArtistsActivity.this, string.noconnection, Toast.LENGTH_LONG).show();        	

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);	
        lv.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        		Intent intentWv = new Intent(getApplicationContext(), WebViewActivity.class);  
        		JSONObject currentObj;
				try {
					currentObj = artistsList.getJSONObject(position);
					intentWv.putExtra("weblink", currentObj.getString("link"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		
        		startActivity(intentWv);
        	}
		});
    }
    

	public void onBackPressed() {
		finish();
		startActivity(new Intent(getApplicationContext(), HomePageActivity.class));
	}    
    
    class BackgroundWorker extends AsyncTask<Void, Void, Void>{
    	ArrayList<HashMap<String, String>> mylist;
    	private ProgressDialog myProgressDialog;
	    @Override
	    protected void onPreExecute() {
	    	// TODO Auto-generated method stub
	    	myProgressDialog=ProgressDialog.show(ArtistsActivity.this, "", ApplicationProperties.ARTISTS_DIALOG_DESC, true);
	    	super.onPreExecute();
	    }
	    
    	@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
            mylist = new ArrayList<HashMap<String, String>>();
                        
            try{   
            	JSONObject json = JSONfunctions.getJSONfromURL(ApplicationProperties.ARTISTI_JSON_URL);
            	artistsList = json.getJSONArray("artist");

    	        for(int i=0;i<artistsList.length();i++){						
    				HashMap<String, String> map = new HashMap<String, String>();	
    				JSONObject e = artistsList.getJSONObject(i);
    				map.put("id",  String.valueOf(i));
    	        	map.put("description", e.getString("description"));
    	        	map.put("link", "link: " +  e.getString("link"));
    	        	mylist.add(map);			
    			}		
            }catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
    	
		@Override
		protected void onPostExecute(Void result)
		{

			Log.d(TAG, "onPostExecute");
	        ListAdapter adapter = new SimpleAdapter(ArtistsActivity.this, mylist , R.layout.jsonartist, 
                    new String[] { "description"}, 
                    new int[] { R.id.item_title });
    
            setListAdapter(adapter);

            myProgressDialog.dismiss();
		}
    }
    
    
    @Override
    protected void onStop() {
    	// TODO Auto-generated method stub
    	super.onStop();
    }
}
